﻿using System.Data;
using Loki.Data;
using Luna.Data.Mappers;

namespace Luna.Data.Security.Mappers
{
    public class UserDataMapper : LunaEntityMapper<UserData, IUserData>
    {
        public const string LOGIN = "Login";
        public const string HASH = "Hash";
        public const string NAME = "Name";
        public const string SURNAME = "Surname";

        public string UserGetByLoginRequest { get; set; }

        protected override void LoadCoreData(IUserData P_Entity, IDataRecord P_Record)
        {
            P_Entity.Login = P_Record.GetString(LOGIN);
            P_Entity.Hash = P_Record.GetString(HASH);
            P_Entity.Name = P_Record.GetString(NAME);
            P_Entity.Surname = P_Record.GetString(SURNAME);
        }

        protected override void MapSaveParameters(IDatabaseCommand P_Command, IUserData P_Entity)
        {
            P_Command.AddParameter(LOGIN, P_Entity.Login);
            P_Command.AddParameter(HASH, P_Entity.Hash);
            P_Command.AddParameter(NAME, P_Entity.Name);
            P_Command.AddParameter(SURNAME, P_Entity.Surname);
            P_Command.AddParameter(IDParameterName, P_Entity.PK);
        }

        internal IUserData GetByLogin(IDbTransaction P_Transaction, string P_Login)
        {
            return ExecuteGet(
                P_Transaction,
                UserGetByLoginRequest,
                c =>
                {
                    c.AddParameter(LOGIN, P_Login);
                });
        }
    }
}
